﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TY.Algorithm
{
    //数据实时稳定算法  
    public class DataRealtimeStability
    {
        /// <summary>
        /// 实例
        /// </summary>
        private List<double> CaleList;
        private List<double> OriginalList;
        private double Coefficient;
        /// <summary>
        /// 实例化对象
        /// </summary>
        /// <param name="_coefficient">计算系数</param>
        public void Init(double _coefficient = 0.5)
        {
            CaleList = new List<double>();
            OriginalList = new List<double>();
            Coefficient = _coefficient;
        }

        /// <summary>
        /// 添加数据，并返回稳定值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public double AppendData(double data)
        {
            OriginalList.Add(data);
            double value = 0;
            if (CaleList.Count == 0)
            {
                value = data;
                CaleList.Add(value);
            }
            else
            {
                value = data - (data - CaleList[CaleList.Count - 1]) * Coefficient;
                CaleList.Add(value);
            }
            return value;
        }

        /// <summary>
        /// 获取数据集合
        /// </summary>
        /// <returns></returns>
        public List<double> GetDataList()
        {
            return CaleList;
        }

        /// <summary>
        /// 获取最大值
        /// </summary>
        /// <returns></returns>
        public double GetMaxValue()
        {
            return CaleList.Max();
        }

        /// <summary>
        /// 获取最小值
        /// </summary>
        /// <returns></returns>
        public double GetMinValue()
        {
            return CaleList.Min();
        }

        /// <summary>
        /// 获取平均值
        /// </summary>
        /// <returns></returns>
        public double GetAverageValue()
        {
            return CaleList.Average();
        }

        /// <summary>
        /// 获取原始数据集合
        /// </summary>
        /// <returns></returns>
        public List<double> GetOriginalDataList()
        {
            return OriginalList;
        }

        /// <summary>
        /// 获取原始数据最大值
        /// </summary>
        /// <returns></returns>
        public double GetOriginalMaxValue()
        {
            return OriginalList.Max();
        }

        /// <summary>
        /// 获取原始数据最小值
        /// </summary>
        /// <returns></returns>
        public double GetOriginalMinValue()
        {
            return OriginalList.Min();
        }
        /// <summary>
        /// 获取原始数据平均值
        /// </summary>
        /// <returns></returns>
        public double GetOriginalAverageValue()
        {
            return OriginalList.Average();
        }

    }
}
